package com.atguigu.eduservice.controller;


import com.atguigu.baseservice.result.R;
import com.atguigu.baseservice.result.ResultCodeEnum;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.vo.ChapterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author dayouzi
 * @since 2022-10-18
 */
@Api(description = "课程章节管理")
@CrossOrigin
@RestController
@RequestMapping("/edu/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation(value = "章节数据列表")
    @GetMapping("nested-list/{courseId}")
    public R nestedListByCourseId(@ApiParam(name = "courseId", value = "课程id", required = true)
                                  @PathVariable String courseId) {
        List<ChapterVo> chapterVos = eduChapterService.nestedList(courseId);
        return R.ok().data("items", chapterVos);
    }

    @ApiOperation(value = "新增章节")
    @PostMapping
    public R save(@ApiParam(name = "chapter", value = "章节对象", required = true)
                  @RequestBody EduChapter chapter) {
        eduChapterService.save(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("{id}")
    public R getById(@ApiParam(name = "id", value = "章节id", required = true)
                     @PathVariable String id) {
        EduChapter eduChapter = eduChapterService.getById(id);
        return R.ok().data("item", eduChapter);
    }

    @ApiOperation(value = "根据id修改章节")
    @PutMapping("{id}")
    public R updateById(@ApiParam(name = "id", value = "章节id", required = true)
                        @PathVariable String id,
                        @ApiParam(name = "chapter", value = "章节对象", required = true)
                        @RequestBody EduChapter chapter) {
        chapter.setId(id);
        eduChapterService.updateById(chapter);
        return R.ok();
    }

    @ApiOperation(value = "根据id删除章节")
    @DeleteMapping("{id}")
    public R deleteById(@ApiParam(name = "id", value = "章节id", required = true)
                        @PathVariable String id) {
        boolean b = eduChapterService.removeChapterById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error(ResultCodeEnum.REMOVE_ERROR);
        }
    }




}

